<?php namespace nimo\domains\car_balance\controllers\crud;

use BaseController;
use Input;
use View;
use Validator;
use Redirect;
use Request;
use Response;

use nimo\domains\car_balance\Model\CarMaker as CarMaker;

class CarMakerController extends BaseController {

    /**
     * CarMaker Repository
     *
     * @var CarMaker
     */
    protected $carMaker;

    public function __construct(CarMaker $carMaker)
    {
        $this->carMaker = $carMaker;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $carMakers = CarMaker::orderBy('node_left')->get();
        if (Request::ajax()) {
            return Response::json($carMakers);
        }
        $nested = array_map(function($carMaker){ return $carMaker->toArray();}, $carMakers->all());
        $jsonCarMaker = json_encode($nested);
        return View::make('nimo.domains.car_balance.CarMaker.index', compact('carMakers', 'jsonCarMaker'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('nimo.domains.car_balance.CarMaker.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, CarMaker::$rules);

        if ($validation->passes())
        {
            $carMaker = null;
            $parentid = Input::get('node_parent');
            if ($parentid > 0 ) {
                $parent = CarMaker::find($parentid);
                if ($parent != null) {
                    $data = Input::only('id', 'name');
                    $carMaker = $this->carMaker->create($input);
                    $carMaker->makeChildOf($parent);
                }
            } else {
                $carMaker = $this->carMaker->create($input);
            }
            if (!is_null($carMaker)) {
                return Redirect::route('car_balance.carmaker.show', $carMaker->id)
                    ->with('success', 'There were successed.');
            }
            return Redirect::route('car_balance.carmaker.create')
                ->withInput()
                ->with('error', 'There were node relation errors.');
        }
        return Redirect::route('car_balance.carmaker.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $carMaker = $this->carMaker->findOrFail($id);

        return View::make('nimo.domains.car_balance.CarMaker.show', compact('carMaker'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $carMaker = $this->carMaker->find($id);

        if (is_null($carMaker))
        {
            return Redirect::route('car_balance.carmaker.index');
        }

        return View::make('nimo.domains.car_balance.CarMaker.edit', compact('carMaker'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, CarMaker::$rules);

        if ($validation->passes())
        {
            $carMaker = $this->carMaker->find($id);
            $parentid = Input::get('node_parent');
            $carMaker->update($input);
            if (!empty($parentid) && $carMaker->node_parent != $parentid) {
                // change parent
                $carMaker->makeChildOf($parentid);
            } else if(empty($parentid) && !empty($carMaker->node_parent)){
                // root
                $carMaker->makeRoot();
            }
            return Redirect::route('car_balance.carmaker.show', $id)
                ->with('success', 'There were successed.');
        }

        return Redirect::route('car_balance.carmaker.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->carMaker->find($id)->delete();

        return Redirect::route('car_balance.carmaker.index');
    }

    /**
     * move up
     *
     * @param  int  $id
     * @return Response
     */
    public function up($id)
    {
        $carMaker = $this->carMaker->find($id);
        if (!is_null($carMaker)) {
            $node = $carMaker->getLeftSibling();
            if (!is_null($node)) {
                $carMaker =  $carMaker->moveToLeftOf($node);
                return Redirect::route('car_balance.carmaker.index')
                    ->with('success', 'There were successed.');
            }
        }

        return Redirect::route('car_balance.carmaker.index');
    }

    /**
     * move down
     *
     * @param  int  $ida
     * @return Response
     */
    public function down($id)
    {
        $carMaker = $this->carMaker->find($id);
        if (!is_null($carMaker)) {
            $node = $carMaker->getRightSibling();
            if (!is_null($node)) {
                $carMaker =  $carMaker->moveToRightOf($node);
                return Redirect::route('car_balance.carmaker.index')
                    ->with('success', 'There were successed.');
            }
        }

        return Redirect::route('car_balance.carmaker.index');
    }

}